﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Globalization;
using System.Linq;
using EstateNuke.Data.Properties;

namespace EstateNuke.Data
{
    public partial class SmallOrder
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        public SmallOrderExpiredAlert LatestUnProcessedSmallOrderExpiredAlert
        {
            get
            {
                return InternalSmallOrderExpiredAlerts.FirstOrDefault(smallOrderExpiredAlert => smallOrderExpiredAlert.IsDeleted == false && smallOrderExpiredAlert.Processed == false);
            }
        }

        /// <summary>
        /// </summary>
        public IList<SmallOrderExpiredAlert> SmallOrderExpiredAlerts
        {
            get
            {
                return InternalSmallOrderExpiredAlerts.Where(smallOrderExpiredAlert => smallOrderExpiredAlert.IsDeleted == false).ToList();
            }
        }

        #endregion

        #region Create & Remove SmallOrderExpiredAlert

        /// <summary>
        /// </summary>
        /// <param name = "alertType"></param>
        /// <param name = "modifiedUser"></param>
        public SmallOrderExpiredAlert CreateSmallOrderExpiredAlert(AlertType alertType, User modifiedUser)
        {
            if (alertType == null)
            {
                throw new ArgumentNullException("alertType");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (alertType.Id != 20)
            {
                throw new ArgumentException("alertType");
            }
            if (!AgreementReference.IsLoaded)
            {
                AgreementReference.Load(MergeOption.PreserveChanges);
            }
            var reminderDays = alertType.DefaultReminderDays.HasValue ? alertType.DefaultReminderDays : 0;
            var alertTypeReminderDays = Agreement.Project.AlertTypeReminderDays;
            if (alertTypeReminderDays != null && alertTypeReminderDays.ContainsKey(alertType) && alertTypeReminderDays[alertType] != null && alertTypeReminderDays[alertType].Item1.HasValue)
            {
                reminderDays = alertTypeReminderDays[alertType].Item1;
            }
            if (reminderDays != 0)
            {
                var smallOrderExpiredAlert = InternalSmallOrderExpiredAlerts.FirstOrDefault(item => item.IsDeleted == false && item.Processed == false);
                if (smallOrderExpiredAlert == null)
                {
                    smallOrderExpiredAlert = new SmallOrderExpiredAlert
                                                 {
                                                     RowId = Guid.NewGuid(),
                                                     CreatedDate = DateTime.Now,
                                                 };
                    InternalSmallOrderExpiredAlerts.Add(smallOrderExpiredAlert);
                }
                smallOrderExpiredAlert.IsDeleted = false;
                smallOrderExpiredAlert.ModifiedDate = DateTime.Now;
                smallOrderExpiredAlert.ModifiedByUser = modifiedUser;
                smallOrderExpiredAlert.AlertType = alertType;
                smallOrderExpiredAlert.RemindDate = ExpiredDate.AddDays((double) reminderDays);
                return smallOrderExpiredAlert;
            }
            return null;
        }

        /// <summary>
        /// </summary>
        /// <param name = "smallOrderExpiredAlert"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveSmallOrderExpiredAlert(SmallOrderExpiredAlert smallOrderExpiredAlert, User modifiedUser)
        {
            if (smallOrderExpiredAlert == null)
            {
                throw new ArgumentNullException("smallOrderExpiredAlert");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalSmallOrderExpiredAlerts.Contains(smallOrderExpiredAlert))
            {
                smallOrderExpiredAlert.Delete(modifiedUser);
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            if (string.IsNullOrEmpty(OrderNumber))
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NotNullAllowed, RowId, "SmallOrder", "OrderNumber"));
            }
            if (ReducedUnitPrice < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "SmallOrder", "ReducedUnitPrice"));
            }
            if (ReducedTotalPrice < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "SmallOrder", "ReducedTotalPrice"));
            }
            if (DiscountRate < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "SmallOrder", "DiscountRate"));
            }
            if (TotalAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "SmallOrder", "TotalAmount"));
            }
            if (DepositAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "SmallOrder", "DepositAmount"));
            }
            if (FirstInstalmentAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "SmallOrder", "FirstInstalmentAmount"));
            }
            OrderNumber = OrderNumber.Length > 32 ? OrderNumber.Substring(0, 32) : OrderNumber;
            Comment = string.IsNullOrEmpty(Comment) ? null : Comment.Length > 512 ? Comment.Substring(0, 512) : Comment;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<SmallOrderExpiredAlert>) SmallOrderExpiredAlerts).ForEach(smallOrderExpiredAlert => smallOrderExpiredAlert.Delete(modifiedUser));
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}